package com.ytjj.quartz.runner;

import com.alibaba.fastjson.JSONObject;
import com.kuaidi100.sdk.contant.RedisConstants;
import com.ytjj.common.service.RedisService;
import com.ytjj.common.utils.HttpClientUtil;

import com.ytjj.qmyx.mall.model.ExpressDic;
import com.ytjj.qmyx.mall.model.PathConfig;
import com.ytjj.quartz.config.SupplyApiConfig;
import com.ytjj.quartz.dao.PathConfigDao;
import com.ytjj.quartz.utils.ExpressDicUtil;
import com.ytjj.quartz.utils.PathCodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @ClassName CacheRunner
 *
 * 
 * @Version 1.0
 **/
@Slf4j
@Component
public class CacheRunner implements ApplicationRunner {
    @Resource
    private RedisService redisService;
    @Resource
    private SupplyApiConfig supplyApiConfig;

    @Resource
    private PathConfigDao pathConfigDao;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("run");
//        List<ExpressDic> expressDicList = expressDao.expressDicList();
//        Map<String, String> collect = expressDicList.stream()
//                .collect(Collectors.toMap(ExpressDic::getExpressKey, ExpressDic::getExpressValue));
//        ExpressDicUtil.putIntoMap(collect);

        Map<String, String> map = new HashMap<>();

        String redisExpressCompanyVal = redisService.get(RedisConstants.EXPRESS_COMPANY);
        if (StringUtils.isNotBlank(redisExpressCompanyVal)){
            List<ExpressDic> yxExpressDicList = JSONObject.parseArray(redisExpressCompanyVal, ExpressDic.class);
            log.info("======map====={}",yxExpressDicList);

            map = yxExpressDicList.stream()
                    .collect(Collectors.toMap(ExpressDic::getExpressKey, ExpressDic::getExpressValue));
        }else {
            try {
                String result = HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/order/getExpressDic", null, supplyApiConfig.getHeadsMap());
                List<ExpressDic> yxExpressDicList = JSONObject.parseArray(result, ExpressDic.class);
                log.info("======map====={}",yxExpressDicList);
                map = yxExpressDicList.stream()
                        .collect(Collectors.toMap(ExpressDic::getExpressKey, ExpressDic::getExpressValue));
                redisService.set(RedisConstants.EXPRESS_COMPANY, JSONObject.toJSONString(yxExpressDicList));
                redisService.expire(RedisConstants.EXPRESS_COMPANY, 60 * 60);
            } catch (Exception e) {}

        }
        ExpressDicUtil.putIntoMap(map);

        Map<String, List<String>> pathConfigMap=new HashMap<>();
        List<PathConfig> pathConfigList=pathConfigDao.getPathCodeList();
        List<String> modulePathList=null;
        for(PathConfig pathConfig:pathConfigList){
            modulePathList=pathConfigMap.get(pathConfig.getModuleCode());
            if(null==modulePathList){
                modulePathList=new ArrayList<>();
            }
            modulePathList.add(pathConfig.getPathCode());
            pathConfigMap.put(pathConfig.getModuleCode(),modulePathList);
        }
        PathCodeUtil.putIntoMap(pathConfigMap);
    }
}